package com.company.project.common.annotation.impl;

import com.company.project.common.annotation.FileTypeValidator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义验证的的实现类
 */
public class FileTypeValidatorClass implements ConstraintValidator<FileTypeValidator, Object> {
    //临时变量保存flag值
    private String values;
    public void initialize(FileTypeValidator constraint) {
        //将注解内配置的值赋给临时变量
        this.values=constraint.values();
    }
    //实现验证
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        //分割定义的有效值
        String[] value_array=values.split(",");
        boolean isFlag=false;
        //遍历比对有效值
        for(int i=0;i<value_array.length;i++){
            //去掉前后空格
            if (value_array[i].trim().equals(value)){
                isFlag=true;
                break;
            }
        }
        //返回是否存在boolean
        return isFlag;
    }
    public boolean isValid(String obj, ConstraintValidatorContext context) {
        return false;
    }
}